import { ElMessage } from "element-plus";
export default function useFile() {
  const urlDownload = function (url, name) {
    var a = document.createElement("a");
    a.setAttribute("href", url);
    a.setAttribute("download", name);
    a.setAttribute("target", "_blank");
    let clickEvent = document.createEvent("MouseEvents");
    clickEvent.initEvent("click", true, true);
    a.dispatchEvent(clickEvent);
  };
  /**
   * @desc 下载
   */
  async function download({ api, senData, fileName = "down" }) {
    try {
      const res = await api(senData || undefined);
      console.info(URL.createObjectURL(res.data));
      await urlDownload(
        URL.createObjectURL(res.data),
        res.headers["content-disposition"].split("=")[1] || fileName
      );
      ElMessage.success("下载成功");
    } catch (error) {
      console.info(error);
      return error;
    }
  }
  return {
    download
  };
}
